#!/bin/bash

# 设置环境变量
DB_HOST=${DB_HOST:-"mysql"}
DB_PORT=${DB_PORT:-"3306"}
DB_NAME=${DB_NAME:-"learn"}
DB_USER=${DB_USER:-"learn"}
ES_HOST=${ES_HOST:-"elasticsearch"}

# 等待MySQL容器创建凭据
MAX_TRIES=30
TRIES=0
CREDENTIALS_PATH="/var/lib/mysql/credentials/mysql_credentials.txt"

echo "等待MySQL容器生成数据库凭据..."
while [ ! -f "$CREDENTIALS_PATH" ] && [ $TRIES -lt $MAX_TRIES ]; do
    sleep 2
    ((TRIES++))
    echo "等待中... ($TRIES/$MAX_TRIES)"
done

if [ -f "$CREDENTIALS_PATH" ]; then
    # 读取MySQL凭据
    DB_PASSWORD=$(grep "数据库密码" "$CREDENTIALS_PATH" | cut -d':' -f2 | tr -d ' ')
    echo "已读取数据库凭据"
    # 复制凭据到可访问位置
    cp "$CREDENTIALS_PATH" /var/www/mysql_credentials.txt
    echo "数据库凭据已复制到 /var/www/mysql_credentials.txt"
else
    echo "无法获取数据库凭据，使用默认密码"
    DB_PASSWORD=${DB_PASSWORD:-"default_password_123"}
fi

# 生成随机JWT密钥
JWT_KEY=$(openssl rand -hex 16)
echo "生成随机JWT密钥: ${JWT_KEY}"

# 创建API环境配置文件
if [ -f /var/www/api/.example.env ] && [ ! -f /var/www/api/.env ]; then
    echo "创建API环境配置文件..."
    cp /var/www/api/.example.env /var/www/api/.env
    # 替换数据库配置
    sed -i "s/HOSTNAME =.*/HOSTNAME = ${DB_HOST}/g" /var/www/api/.env
    sed -i "s/DATABASE =.*/DATABASE = ${DB_NAME}/g" /var/www/api/.env
    sed -i "s/USERNAME =.*/USERNAME = ${DB_USER}/g" /var/www/api/.env
    sed -i "s/PASSWORD =.*/PASSWORD = ${DB_PASSWORD}/g" /var/www/api/.env
    sed -i "s/HOSTPORT =.*/HOSTPORT = ${DB_PORT}/g" /var/www/api/.env
    # 替换ES配置
    sed -i "s/HOST =.*/HOST = ${ES_HOST}/g" /var/www/api/.env
    # 替换JWT密钥
    sed -i "s/KEY =.*/KEY = ${JWT_KEY}/g" /var/www/api/.env
fi

# 配置Admin项目的数据库连接（可能使用不同的配置方式）
if [ -d /var/www/admin ]; then
    echo "配置Admin数据库连接..."
    # 如果admin使用.env文件，则创建
    if [ -f /var/www/admin/.example.env ] && [ ! -f /var/www/admin/.env ]; then
        cp /var/www/admin/.example.env /var/www/admin/.env
        sed -i "s/HOSTNAME =.*/HOSTNAME = ${DB_HOST}/g" /var/www/admin/.env
        sed -i "s/DATABASE =.*/DATABASE = ${DB_NAME}/g" /var/www/admin/.env
        sed -i "s/USERNAME =.*/USERNAME = ${DB_USER}/g" /var/www/admin/.env
        sed -i "s/PASSWORD =.*/PASSWORD = ${DB_PASSWORD}/g" /var/www/admin/.env
        sed -i "s/HOSTPORT =.*/HOSTPORT = ${DB_PORT}/g" /var/www/admin/.env
        # 同样使用相同的JWT密钥
        sed -i "s/KEY =.*/KEY = ${JWT_KEY}/g" /var/www/admin/.env
    fi
    
    # 如果没有发现.env文件，则修改数据库配置文件
    if [ -f /var/www/admin/application/database.php ]; then
        echo "使用环境变量更新Admin数据库配置..."
    fi
fi

# 创建runtime目录并设置权限
for DIR in /var/www/api/runtime /var/www/admin/runtime; do
    if [ -d "$(dirname "$DIR")" ] && [ ! -d "$DIR" ]; then
        mkdir -p "$DIR"
        echo "创建目录: $DIR"
    fi
    if [ -d "$DIR" ]; then
        chmod -R 755 "$DIR"
        chown -R www-data:www-data "$DIR"
        echo "设置目录权限: $DIR"
    fi
done

# 启动服务
echo "启动服务..."
if [ -d /var/www/api ]; then
    cd /var/www/api
    if [ -f "think" ]; then
        echo "停止已运行的任务..."
        php think task stop 2>/dev/null || true
        sleep 3
        echo "启动任务..."
        php think task start
    fi
fi

# 执行supervisord保持容器运行
echo "启动supervisord..."
exec /usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf 